import { insertComponent } from './methods'

export const casePlugin = (editor) => {
  const config = [
    {
      type: 'addButton',
      name: 'input',
      config: {
        text: '输入框',
        tooltip: '输入框',
        onAction: function (_) {
          insertComponent('input')
        }
      }
    },
    {
      type: 'addButton',
      name: 'radio',
      config: {
        text: '单选框',
        tooltip: '单选框',
        onAction: function (_) {
          insertComponent('radio')
        }
      }
    },
    {
      type: 'addButton',
      name: 'checkbox',
      config: {
        text: '多选框',
        tooltip: '多选框',
        onAction: function (_) {
          insertComponent('checkbox')
        }
      }
    },
    {
      type: 'addSplitButton',
      name: 'select',
      config: {
        text: '下拉框',
        tooltip: '下拉框',
        fetch: function (callback) {
          const items = [
            {
              type: 'choiceitem',
              text: '单选下拉',
              value: 'single'
            },
            {
              type: 'choiceitem',
              text: '多选下拉',
              value: 'multiple'
            }
          ]
          callback(items)
        },
        onAction: function (_) {
          console.log(1)
          insertComponent('select')
        },
        onItemAction: function (buttonApi, value) {
          console.log(buttonApi)
          console.log(value)
        }
      }
    },
    {
      type: 'addSplitButton',
      name: 'timeinput',
      config: {
        text: '时间输入框',
        tooltip: '时间输入框',
        fetch: function (callback) {
          const items = [
            {
              type: 'choiceitem',
              text: 'yyyy-MM-dd hh:mm:ss',
              value: 'yyyy-MM-dd hh:mm:ss'
            },
            {
              type: 'choiceitem',
              text: 'yyyy-MM-dd',
              value: 'yyyy-MM-dd'
            },
            {
              type: 'choiceitem',
              text: 'hh:mm:ss',
              value: 'hh:mm:ss'
            }
          ]
          callback(items)
        },
        onAction: function (_) {
          console.log(1)
          insertComponent('select')
        },
        onItemAction: function (buttonApi, value) {
          console.log(buttonApi)
          console.log(value)
        }
      }
    },
    {
      type: 'addButton',
      name: 'region',
      config: {
        text: '控件域',
        tooltip: '控件域',
        onAction: function (_) {
          insertComponent('region')
        }
      }
    }
  ]
  config.forEach((v) => {
    editor.ui.registry[v.type](v.name, v.config)
  })
}
